//**************************************************************
//		AHPreferences.m
//			AppHider
//
//	Copyright (c) 2008 Metta Media
//
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//**************************************************************
#import "AHPreferences.h"


@implementation AHPreferences
- (id) init
{
	self = [super init];
	if (self != nil) {
		appID = CFSTR("com.mettamedia.AppHider");
	}
	return self;
}

-(void)awakeFromNib
{
	[self savePrefs];
}

-(void)dealloc
{
	[self savePrefs];
	[super dealloc];
}

-(void)savePrefs
{
	// Write out the preference data.
	CFPreferencesAppSynchronize(appID);
}

- (NSString*)getStringFromKey:(CFStringRef)key
{
	NSString * string = @"";
		
	CFStringRef stringRef = CFPreferencesCopyAppValue(key, appID);
	if(stringRef)
	{
		string = (NSString*)stringRef;
		CFRelease(stringRef);
	}
	return string;
}

- (BOOL)getBoolFromKey:(CFStringRef)key
{
	CFPropertyListRef value;
	BOOL ret = YES;
	
	value = CFPreferencesCopyAppValue(key, appID);
	if(value && CFGetTypeID(value) == CFBooleanGetTypeID())
		ret = CFBooleanGetValue(value);
	
	if(value)
		CFRelease(value);
	
	return ret;
}

- (NSMutableArray*)getArrayFromKey:(CFStringRef)key
{
	NSMutableArray * arr = [[NSMutableArray alloc] init];
	
	CFArrayRef arrayRef = (CFArrayRef)CFPreferencesCopyAppValue(key, appID);
	if(arrayRef) 
	{
		[arr setArray:(NSArray*)arrayRef];
		CFRelease(arrayRef);
	}
	return arr;
}

- (void)setKey:(CFStringRef)key fromBool:(BOOL)value
{
	CFPreferencesSetAppValue(key, (value ? kCFBooleanTrue : kCFBooleanFalse), appID);
}

- (void)setKey:(CFStringRef)key fromArray:(NSArray*)value
{
	CFPreferencesSetAppValue(key, (CFArrayRef)value, appID);
}

- (void)setKey:(CFStringRef)key fromString:(NSArray*)value
{
	CFPreferencesSetAppValue(key, (CFStringRef)value, appID);
}
@end
